Any C++ pros out there?

Kinja'd!!! "If only EssExTee could be so grossly incandescent" (essextee)
08/31/2014 at 01:36 • Filed to: None

Kinja'd!!!0 Kinja'd!!! 20

I'm re-teaching myself after a ~2 year break and I'd love to pick your brain for help with a program I'm writing.


DISCUSSION (20)


Kinja'd!!! mcseanerson > If only EssExTee could be so grossly incandescent
08/31/2014 at 01:43

Kinja'd!!!1

Wish I could help but I still haven't learned the first time. Re-learning sucks because your reading through and it's like 30 pages of I know that and when you're finally tired of reading every single sentence you skip a little bit and then miss the one thing that is either new or you didn't remember. I do not envy you.


Kinja'd!!! Tim (Fractal Footwork) > If only EssExTee could be so grossly incandescent
08/31/2014 at 01:45

Kinja'd!!!0

I'm in the same boat as you, but what is your question anyhow? I don't know if I can help, but perhaps...


Kinja'd!!! Nate Bleker > If only EssExTee could be so grossly incandescent
08/31/2014 at 01:45

Kinja'd!!!1

I might be a bit drunk but I do c++ for a living. Whatcha got?


Kinja'd!!! If only EssExTee could be so grossly incandescent > Nate Bleker
08/31/2014 at 01:55

Kinja'd!!!0

Well, let's say the program is waiting on input from the user. The input options are 1 and 2. If the user types those numbers, the program continues, but if anything else is input the program quits. What can I do so that if a "wrong" input is given, the request for input is repeated?


Kinja'd!!! ToyDeathbot > If only EssExTee could be so grossly incandescent
08/31/2014 at 02:09

Kinja'd!!!0

can't you use a 'while' loop to constantly check if the input is false?


Kinja'd!!! If only EssExTee could be so grossly incandescent > ToyDeathbot
08/31/2014 at 02:33

Kinja'd!!!0

I'm getting some super weird error messages when trying to use it, such as "variable is being used without being initialized". Do I have to initialize it within the loop, as well as before my main()?


Kinja'd!!! ToyDeathbot > If only EssExTee could be so grossly incandescent
08/31/2014 at 02:50

Kinja'd!!!0

I'm not particularly familiar with C++, but in general, the layout should be something like this.

User inputs something (let's say x) <- This is where I think your error came from
while x > 2 | x == 0
give some sort of error message and tell the user to re-input x value
end

and then if it is option 1 or 2, it continues onwards


Kinja'd!!! ToyDeathbot > If only EssExTee could be so grossly incandescent
08/31/2014 at 03:00

Kinja'd!!!0

here's something I've done real quick on MATLAB (it's way easier to understand than C++)

Kinja'd!!!


Kinja'd!!! If only EssExTee could be so grossly incandescent > ToyDeathbot
08/31/2014 at 03:02

Kinja'd!!!1

Fucking MATLAB...

*grumbles*


Kinja'd!!! ToyDeathbot > If only EssExTee could be so grossly incandescent
08/31/2014 at 03:21

Kinja'd!!!0

haha, that's what everyone says


Kinja'd!!! Pockets > If only EssExTee could be so grossly incandescent
08/31/2014 at 06:47

Kinja'd!!!0

When you say "initialising before main()", that sounds kinda dodgy - is it declared but not initialised at that point, then only gets initialised within the loop, which would mean on the first iteration it would fail like this? Post a snippet.


Kinja'd!!! crowmolly > If only EssExTee could be so grossly incandescent
08/31/2014 at 08:38

Kinja'd!!!0

Probably happening if you are using a while loop without giving the variable an initial value.

If you say

while (x != 0)

{

}

it won't work unless you do something like

int x = 9999;

while (x != 0)
{

}

In the first case, x will be filled with whatever shit currently resides in the memory address that was just given to x. It could be anything at that point.

Or you could use a do/while loop. In that case the condition is checked at the END of the loop so the inner code (where you probably assign a value to the variable giving you trouble).


Kinja'd!!! zeontestpilot > If only EssExTee could be so grossly incandescent
08/31/2014 at 09:36

Kinja'd!!!0

Um, I use Java, and haven't touched C++ in a while. I know in Java, you have to initialize the variable before the loop, at least if you want to use it out side of it. This is a mix of java & C++, so....

int x = 0;

while (true) {

if (x == 1) { break; }

else if (x == 2 ) { break; }

else { cout << "Incorrect input, try again." }

} // end while loop.

You could mske the if statement like "if( x==1 || x ==2 )", but if you give it just one statement to check, its more of a boolean statement and should run faster (better on bigger programs).

Can you post your code so we can see what you have so far?


Kinja'd!!!  > If only EssExTee could be so grossly incandescent
08/31/2014 at 10:46

Kinja'd!!!0

I will give you one tip that will save your ass every time. Don't forget a semicolon.


Lol ok that's all I got. I hate programming but can do it if I have to.


Kinja'd!!!  > ToyDeathbot
08/31/2014 at 10:50

Kinja'd!!!1

That looks like c#, not c++. In c++ you have to initialize the variable before you can use it.

it would be something like

int x;

Assuming x is an integer. You can read on variables here, http://www.cplusplus.com/doc/tutorial/v…


Kinja'd!!! Tim (Fractal Footwork) > If only EssExTee could be so grossly incandescent
08/31/2014 at 11:54

Kinja'd!!!0

Don't you have to put in an else at the end?

if, logical statement if a

else if, logical statement if b

else, logical statement if not either a or b


Kinja'd!!! If only EssExTee could be so grossly incandescent > crowmolly
08/31/2014 at 12:00

Kinja'd!!!0

The variable is declared at the beginning already. That's why the error message makes no sense.


Kinja'd!!! If only EssExTee could be so grossly incandescent > Pockets
08/31/2014 at 12:03

Kinja'd!!!0

Problem is, the code works perfectly fine without the loop, but the moment I add the do{}while it stops working.


Kinja'd!!! crowmolly > If only EssExTee could be so grossly incandescent
08/31/2014 at 16:10

Kinja'd!!!1

Declared AND initialized?


Kinja'd!!! If only EssExTee could be so grossly incandescent > crowmolly
08/31/2014 at 21:43

Kinja'd!!!0

The variable's value is created by input from the user, there is no initial value. The do while loop is supposed to allow for that but it's not.